package com.lanou3g.mynewstest.module.videos;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.base.MyBaseFragment;
import com.lanou3g.mynewstest.beans.VideoBeans;
import com.lanou3g.mynewstest.tools.MyRequestQueue;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/7.
 */
public class VideoFragment extends MyBaseFragment {
    private VideoBeans beans;
    private int imageViewID[] = {R.id.video_sidlist_iv1, R.id.video_sidlist_iv2,
            R.id.video_sidlist_iv3, R.id.video_sidlist_iv4};
    private int textViewID[] = {R.id.video_sidlist_tv1, R.id.video_sidlist_tv2,
            R.id.video_sidlist_tv3, R.id.video_sidlist_tv4};
    private NetworkImageView imageView;
    private TextView textView;
    private List<NetworkImageView> imageViews;
    private List<TextView> textViews;

    private ListView listView;
    private VideoAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_video, container, false);
        listView = (ListView) view.findViewById(R.id.video_listview);


        getBeans();
        return view;
    }

    private void getBeans() {

        StringRequest request = new StringRequest("http://c.3g.163.com/nc/video/home/0-10.html",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        beans = gson.fromJson(response, VideoBeans.class);
                        //头布局
                        View headView = LayoutInflater.from(context).inflate(R.layout.item_video_head, null);
                        imageViews = new ArrayList<>();
                        textViews = new ArrayList<>();
                        for (int i = 0; i < 4; i++) {
                            imageView = (NetworkImageView) headView.findViewById(imageViewID[i]);
                            textView = (TextView) headView.findViewById(textViewID[i]);
                            imageViews.add(imageView);
                            textViews.add(textView);
                        }
                        for (int i = 0; i < 4; i++) {
                            MyRequestQueue.setNetImage(imageViews.get(i), beans.getVideoSidList().get(i).getImgsrc());
                            textViews.get(i).setText(beans.getVideoSidList().get(i).getTitle());
                        }
                        listView.addHeaderView(headView);

                        adapter = new VideoAdapter(beans, context);
                        listView.setAdapter(adapter);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        //单例模式的requestqueue
        MyRequestQueue.addRequest(request);
    }
}
